package styledaco.board;

import java.io.UnsupportedEncodingException;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;

import statics.SoptVars;
import styledaco.member.MemberProc;

import com.ibatis.sqlmap.client.SqlMapClient;
import comm.util.CommUtil;
import comm.util.RequestManager;
import comm.util.SoptHashMap;
import comm.util.SqlMapLoader;

public class ReplyProc {
	private SqlMapClient sqlMapper = SqlMapLoader.getInstance();
	private Logger logger = Logger.getLogger(ReplyProc.class);
	
	public String regReply(HttpServletRequest request) throws UnsupportedEncodingException{
		SoptHashMap hm = RequestManager.getRequest(request);
		hm.put("MEMBER_SEQ", request.getSession().getAttribute("SESSION_MEMBER_SEQ"));
		String result = SoptVars.FAIL;
		try {
			result = CommUtil.NVL(sqlMapper.insert("reply.regReply",hm),"");
			if(result.length()>0)result = "1";
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return result;
	}
	
	public String updateReply(HttpServletRequest request) throws UnsupportedEncodingException{
		SoptHashMap hm = RequestManager.getRequest(request);
		hm.put("MEMBER_SEQ", request.getSession().getAttribute("SESSION_MEMBER_SEQ"));
		String result = SoptVars.FAIL;
		try {
			result = CommUtil.NVL(sqlMapper.update("reply.updateReply", hm),"");
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return result;
	}
	
	public String deleteReply(HttpServletRequest request) throws UnsupportedEncodingException{
		SoptHashMap hm = RequestManager.getRequest(request);
		hm.put("MEMBER_SEQ", request.getSession().getAttribute("SESSION_MEMBER_SEQ"));
		String result = SoptVars.FAIL;
		try {
			result = CommUtil.NVL(sqlMapper.update("reply.deleteReply", hm),"");
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return result;
	}
	
	
	public ArrayList getReplyListByBoardSeq(String boardSeq,int curPage,int pageSize){
		ArrayList lt = null;
		
		try{
			if(boardSeq.length()>0){
				SoptHashMap hm = new SoptHashMap();
				hm.put("BOARD_SEQ",boardSeq);
				hm.put("PAGE_SIZE",pageSize<1?SoptVars.PAGE_SIZE_REPLY:pageSize);
				hm.put("PAGE",(curPage-1)*(pageSize<1?SoptVars.PAGE_SIZE_REPLY:pageSize));
				
				lt = (ArrayList) sqlMapper.queryForList("reply.getReplyListByBoardSeq",hm);
			}
		}catch (Exception e) {
			return lt;
		}
		
		return lt;
	}
}
